package bee2d.display;
import nme.display.BitmapData;
import nme.display.Bitmap;
import nme.display.SimpleButton;
import nme.geom.Point;
import nme.geom.Rectangle;

/**
 * ...
 * @author wangjian QQ:421924396
 */

class Button  extends SimpleButton
{
	public function new(_bitmapData:BitmapData,upRect:Rectangle=null,overRect:Rectangle=null,downRect:Rectangle=null,hitRect:Rectangle=null) 
	{
		if (_bitmapData == null) {
			super();
			return;
		}
		var upBitmap:Bitmap=null;
		var overBitmap:Bitmap=null;
		var downBitmap:Bitmap=null;
		var hitBitmap:Bitmap=null;
		var point:Point = new Point();
		if (upRect!=null) {
			var upBitmapData:BitmapData = new BitmapData(Std.int(upRect.width), Std.int(upRect.height));
			upBitmapData.copyPixels(_bitmapData, upRect, point);
			upBitmap = new Bitmap(upBitmapData);
			upBitmap.smoothing = true;
		}
		if (overRect!=null) {
			var overBitmapData:BitmapData= new BitmapData(Std.int(overRect.width), Std.int(overRect.height));
			overBitmapData.copyPixels(_bitmapData, overRect, point);
			overBitmap = new Bitmap(overBitmapData);
			overBitmap.smoothing = true;
		}
		if (downRect!=null) {
			var downBitmapData:BitmapData= new BitmapData(Std.int(downRect.width), Std.int(downRect.height));
			downBitmapData.copyPixels(_bitmapData, downRect, point);
			downBitmap = new Bitmap(downBitmapData);
			downBitmap.smoothing = true;
		}
		if (hitRect!=null) {
			var hitBitmapData:BitmapData= new BitmapData(Std.int(hitRect.width), Std.int(hitRect.height));
			hitBitmapData.copyPixels(_bitmapData, hitRect, point);
			hitBitmap= new Bitmap(hitBitmapData);
			hitBitmap.smoothing = true;
		}
		super(upBitmap, overBitmap, downBitmap, hitBitmap);
	}
	public static function createButton(_bitmapData:BitmapData, width:Int, height:Int):Button {
		if (_bitmapData == null) return null;
		if (_bitmapData.height > height * 2) {
			return new Button(_bitmapData, new Rectangle(0, 0, width, height),new Rectangle(0, height, width, height),new Rectangle(0, height*2, width, height),new Rectangle(0, height*3, width, height));
		}else if (_bitmapData.width > width * 2) {
			return new Button(_bitmapData, new Rectangle(0, 0, width, height),new Rectangle(width, 0, width, height),new Rectangle(width*2, 0, width, height),new Rectangle(width*3, 0, width, height));
		}else {
			return new Button(_bitmapData, new Rectangle(0, 0, width, height));
		}
		return null;
	}
}